package njiandan.mvc.core;

import java.util.HashSet;
import java.util.Set;

import njiandan.mvc.annotation.BaseMapping;
import njiandan.mvc.annotation.InterceptorMapping;
import njiandan.mvc.config.Const;
import njiandan.mvc.utils.ClassScaner;

class ComponentScaner {

	Set<Interceptor> getInteceptorBeans(Config config) {
		ClassScaner sc = new ClassScaner(config.getClass().getClassLoader());

		String param = config.getInitParameter(Const.INITPARA_ACTION_PACKAGES);

		if (param != null && !param.equals("")) {
			Set<String> componentNames = new HashSet<String>();
			for (String pkg : param.split(";")) {
				componentNames.addAll(sc.getClassnameFromPackage(pkg.trim(),
						true));
			}

			Class<?> clazz;
			Set<Class<?>> interceptorClasses = new HashSet<Class<?>>();
			for (String clsName : componentNames) {
				try {
					clazz = Class.forName(clsName, false, this.getClass()
							.getClassLoader()); // 避免static语句执行所发生的错误
					if (clazz.getAnnotation(InterceptorMapping.class) != null) {
						for (Class<?> parent = clazz.getSuperclass(); parent != null; parent = parent
								.getSuperclass()) {
							if (parent.equals(Interceptor.class)) {
								interceptorClasses.add(clazz);
								break;
							}
						}
					}
				} catch (Throwable e) {
					e.printStackTrace();
				}
			}

			/**/
			Set<Interceptor> interceptors = new HashSet<Interceptor>();
			Interceptor itcep = null;
			for (Class<?> cls : interceptorClasses) {
				try {
					itcep = (Interceptor) cls.newInstance();
					if (itcep.initMatcher()) {
						interceptors.add(itcep);
					}
				} catch (InstantiationException e) {
					e.printStackTrace();
				} catch (IllegalAccessException e) {
					e.printStackTrace();
				}
			}

			return interceptors;
		}

		return null;
	}

	/**
	 * Find all beans in container.
	 */
	Set<Object> getActionBeans(Config config) {
		String param = config.getInitParameter(Const.INITPARA_ACTION_PACKAGES);
		ClassScaner sc = new ClassScaner(config.getClass().getClassLoader());

		if (param != null && !param.equals("")) {
			Set<Class<?>> actionClasses = new HashSet<Class<?>>();
			String[] packages = param.split(";");
			Set<String> componentNames = new HashSet<String>();
			for (String pkg : packages) {
				componentNames.addAll(sc.getClassnameFromPackage(pkg.trim(),
						true));
			}

			Class<?> clazz;
			for (String clsName : componentNames) {
				try {
					clazz = Class.forName(clsName, false,
							ComponentScaner.class.getClassLoader()); // 避免static语句执行所发生的错误
					if (clazz.getAnnotation(BaseMapping.class) != null) {
						actionClasses.add(clazz);
					}
				} catch (Throwable e) {
					e.printStackTrace();
				}
			}

			Set<Object> actions = new HashSet<Object>();
			for (Class<?> cls : actionClasses) {
				try {
					actions.add(cls.newInstance());
				} catch (InstantiationException e) {
					e.printStackTrace();
				} catch (IllegalAccessException e) {
					e.printStackTrace();
				}
			}

			return actions;
		}

		return null;
	}
}
